package com.jandan.logic;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jandan.persistence.iface.AccountDao;
import com.jandan.persistence.iface.ArticleDao;
import com.jandan.persistence.iface.BlogArticleDao;
import com.jandan.persistence.iface.LearnedWordDao;
import com.jandan.persistence.iface.LessonDao;
import com.jandan.persistence.iface.WordCommentDao;
import com.jandan.persistence.iface.WordDao;
import com.jandan.persistence.iface.WordLibDao;
import com.jandan.ui.client.util.ClientUtil;
import com.jandan.ui.model.Account;
import com.jandan.ui.model.Article;
import com.jandan.ui.model.BlogArticle;
import com.jandan.ui.model.LearnedWord;
import com.jandan.ui.model.Lesson;
import com.jandan.ui.model.ReviewInfo;
import com.jandan.ui.model.Word;
import com.jandan.ui.model.WordComment;
import com.jandan.ui.model.WordLib;

/**
 * JWordzFacade的实现类
 * @see JWordzFacade
 * @author Gong Yong
 * @version 1.0
 */
public class JWordzImpl implements JWordzFacade {
	/**
	 * 用于操作账户的对象
	 */
	private AccountDao accountDao;
	/**
	 * 用于操作词库的对象
	 */
	private WordLibDao wordLibDao;
	/**
	 * 用于单词的对象
	 */
	private WordDao wordDao;
	/**
	 * 用于煎蛋文章的对象
	 */
	private ArticleDao articleDao;
	/**
	 * 用于操作课程的对象
	 */
	private LessonDao lessonDao;
	/**
	 * 用于操作已学单词的对象
	 */
	private LearnedWordDao learnedWordDao;
	/**
	 * 用于操作单词评论的对象
	 */
	private WordCommentDao wordCommentDao;
	/**
	 * 用于操作博客文章的对象
	 */
	private BlogArticleDao blogArticleDao;
	/**
	 * 用于导入词库的对象
	 */
	private WordImportTool wordImportTool;

	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public void setWordLibDao(WordLibDao wordLibDao) {
		this.wordLibDao = wordLibDao;
	}

	public void setWordDao(WordDao wordDao) {
		this.wordDao = wordDao;
	}

	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	public void setBlogArticleDao(BlogArticleDao blogArticleDao) {
		this.blogArticleDao = blogArticleDao;
	}

	public void setLessonDao(LessonDao lessonDao) {
		this.lessonDao = lessonDao;
	}

	public void setLearnedWordDao(LearnedWordDao learnedWordDao) {
		this.learnedWordDao = learnedWordDao;
	}

	public void setWordCommentDao(WordCommentDao wordCommentDao) {
		this.wordCommentDao = wordCommentDao;
	}

	public void setWordImportTool(WordImportTool wordImportTool) {
		this.wordImportTool = wordImportTool;
	}

	public Account getAccount(String userName) {
		// TODO Auto-generated method stub
		return accountDao.getAccountByUserName(userName);
	}

	public Account getAccount(String userName, String password) {
		// TODO Auto-generated method stub
		return accountDao.getAccountByUserNameAndPassword(userName, password);
	}
	

	public int getTotalAccountCount() {
		// TODO Auto-generated method stub
		return accountDao.getTotalAccountCount();
	}


	public Account getAccountByUserID(long userID) {
		// TODO Auto-generated method stub
		return accountDao.getAccountByUserID(userID);
	}

	public List<Account> getAllAccountList() {
		// TODO Auto-generated method stub
		return accountDao.getAllAccountList();
	}
	
	public List<Account> getAllAccountList(int start,int limit) {
		// TODO Auto-generated method stub
		return accountDao.getAllAccountList(start,limit);
	}

	public void insertAccount(Account account) {
		// TODO Auto-generated method stub
		accountDao.insertAccount(account);
	}

	public void updateAccount(Account account) {
		// TODO Auto-generated method stub
		accountDao.updateAccount(account);
	}

	public void deleteAccount(long userID) {
		// TODO Auto-generated method stub
		accountDao.deleteAccount(userID);
	}

	public Word getWordByWordEn(String wordEn) {
		// TODO Auto-generated method stub
		return wordDao.getWordByWordEn(wordEn);
	}

	public Word getWordByWordID(long wordID) {
		// TODO Auto-generated method stub
		return wordDao.getWordByWordID(wordID);
	}

	public void insertStrangeWord(Word word, long userID) {
		// TODO Auto-generated method stub
		wordDao.insertStrangeWord(word, userID);
	}

	public List<Word> getStrangeWordListByUserID(long userID) {
		// TODO Auto-generated method stub
		return wordDao.getStrangeWordListByUserID(userID);
	}

	public List<Word> getStrangeWordListByUserID(long userID, int offset,
			int limit) {
		// TODO Auto-generated method stub
		return wordDao.getStrangeWordListByUserID(userID, offset, limit);
	}

	public int getStrangeWordCountByUserID(long userID) {
		// TODO Auto-generated method stub
		return wordDao.getStrangeWordCountByUserID(userID);
	}
	public List<WordLib> getAllWordLibList() {
		// TODO Auto-generated method stub
		return wordLibDao.getAllUserWordLibList();
	}

	public Word getWordOfOnePositionByWordLibID(long wordLibID, int position) {
		// TODO Auto-generated method stub
		return wordDao.getWordOfOnePositionByWordLibID(wordLibID, position);
	}
	

	public Word getStrangeWordOfOnePositionByUserID(long userID,
			int position) {
		// TODO Auto-generated method stub
		return wordDao.getStrangeWordOfOnePositionByUserID(userID, position);
	}


	public int getSearchedWordCount(String word,long wordLibID) {
		// TODO Auto-generated method stub
		return wordDao.getSearchedWordCount(word,wordLibID);
	}

	public List<Word> getSearchedWordList(String word, int offset, int limit,long wordLibID) {
		// TODO Auto-generated method stub
		return wordDao.getSearchedWordList(word, offset, limit,wordLibID);
	}

	public WordLib getWordLibByWordLibID(long wordLibID) {
		// TODO Auto-generated method stub
		return wordLibDao.getWordLibByWordLibID(wordLibID);
	}

	public void insertWordLib(WordLib wordLib) {
		// TODO Auto-generated method stub
		wordLibDao.insertWordLib(wordLib);
	}

	public void updateWordLib(WordLib wordLib) {
		// TODO Auto-generated method stub
		wordLibDao.updateWordLib(wordLib);
	}

	public void deleteWordLib(long wordLibID) {
		// TODO Auto-generated method stub
		wordLibDao.deleteWordLib(wordLibID);
	}

	public void importWords(String[] words, long wordLibID) {
		// TODO Auto-generated method stub
		wordImportTool.importWords(words, wordLibID);
	}

	public List<Word> getAllWordListByWordLibID(long wordLibID) {
		// TODO Auto-generated method stub
		return wordDao.getWordListByWordLibID(wordLibID);
	}

	public List<Word> getWordListByWordLibID(long wordLibID, int offset,
			int limit) {
		// TODO Auto-generated method stub
		return wordDao.getWordListByWordLibID(wordLibID, offset, limit);
	}

	public int getTotalWordCountOfWordLib(long wordLibID) {
		// TODO Auto-generated method stub
		return wordDao.getTotalWordCountOfWordLib(wordLibID);
	}

	public List<Article> getAllArticleList() {
		// TODO Auto-generated method stub
		return articleDao.getAllArticleList();
	}

	public List<Article> getLatestJandanArticleList(int n) {
		// TODO Auto-generated method stub
		return articleDao.getLatestJandanArticleList(n);
	}

	public Article getArticleByArticleID(long articleID) {
		// TODO Auto-generated method stub
		return articleDao.getArticleByArticleID(articleID);
	}

	public void insertArticle(Article article) {
		// TODO Auto-generated method stub
		articleDao.insertArticle(article);
	}

	public void updateArticle(Article article) {
		// TODO Auto-generated method stub
		articleDao.updateArticle(article);
	}

	public void deleteArticle(long articleID) {
		// TODO Auto-generated method stub
		articleDao.deleteArticle(articleID);
	}

	public List<Lesson> getLessonListByUserID(long userID) {
		// TODO Auto-generated method stub
		return lessonDao.getLessonListByUserID(userID);
	}

	public Lesson getLessonByLessonID(long lessonID) {
		// TODO Auto-generated method stub
		return lessonDao.getLessonByLessonID(lessonID);
	}

	public long insertLesson(Lesson lesson) {
		// TODO Auto-generated method stub
		return lessonDao.insertLesson(lesson);
	}

	public void deleteLesson(long lessonID) {
		// TODO Auto-generated method stub
		lessonDao.deleteLesson(lessonID);
	}

	public void insertOrUpdateLearnedWord(LearnedWord learnedWord) {
		// TODO Auto-generated method stub
		learnedWordDao.insertOrUpdateLearnedWord(learnedWord);
	}

	public List<Word> getWordListByLessonIDAndLibNumber(long lessonID,
			int libNumber) {
		// TODO Auto-generated method stub
		List<Word> wordList = new ArrayList<Word>();
		Lesson lesson = lessonDao.getLessonByLessonID(lessonID);
		if (lesson != null) {
			long wordLibID = lesson.getWordLibID();
			int smallWordCount = lesson.getWordsCountOfPerSmallLib();

			int offset = libNumber * smallWordCount;
			int limit = smallWordCount;

			wordList = wordDao.getWordListByWordLibID(wordLibID, offset, limit);
		}
		return wordList;
	}

	public List<Word> getNewWordList(long lessonID, int libNumber) {
		// TODO Auto-generated method stub
		List<Word> wordList = new ArrayList<Word>();
		Lesson lesson = lessonDao.getLessonByLessonID(lessonID);
		if (lesson != null) {
			long wordLibID = lesson.getWordLibID();
			int smallWordCount = lesson.getWordsCountOfPerSmallLib();

			int offset = libNumber * smallWordCount;
			int limit = smallWordCount;

			wordList = wordDao.getNewWordListByWordLibID(lessonID, wordLibID,
					offset, limit);
		}
		return wordList;
	}

	public List<LearnedWord> getLearnedWordListByLessonID(long lessonID) {
		// TODO Auto-generated method stub
		return learnedWordDao.getLearnedWordListByLessonID(lessonID);
	}

	public List<LearnedWord> getLearnedWordListByLessonIDAndLibNumber(
			long lessonID, int libNumber) {
		// TODO Auto-generated method stub
		Lesson lesson = lessonDao.getLessonByLessonID(lessonID);

		int n = lesson.getWordsCountOfPerSmallLib();
		long wordLibID = lesson.getWordLibID();

		int offset = libNumber * n;
		int limit = n;

		return learnedWordDao.getLearnedWordListOfOneSmallLibByLessonID(
				lessonID, wordLibID, offset, limit);
	}

	public List<LearnedWord> getLearnedWordListByLessonIDAndLibNumber(
			long lessonID, int libNumber, int start, int count) {
		// TODO Auto-generated method stub
		Lesson lesson = lessonDao.getLessonByLessonID(lessonID);

		int n = lesson.getWordsCountOfPerSmallLib();
		long wordLibID = lesson.getWordLibID();

		int offset = libNumber * n;
		int limit = n;

		return learnedWordDao.getLearnedWordListOfOneSmallLibByLessonID(
				lessonID, wordLibID, offset, limit, start, count);
	}

	public int getLearnedWordCountByLessonIDAndLibNumber(long lessonID,
			int libNumber) {
		// TODO Auto-generated method stub
		Lesson lesson = lessonDao.getLessonByLessonID(lessonID);

		int n = lesson.getWordsCountOfPerSmallLib();
		long wordLibID = lesson.getWordLibID();

		int offset = libNumber * n;
		int limit = n;

		return learnedWordDao.getLearnedWordCountOfOneSmallLibByLessonID(
				lessonID, wordLibID, offset, limit);
	}

	public List<BlogArticle> getAllBlogArticleList() {
		// TODO Auto-generated method stub
		return blogArticleDao.getAllBlogArticleList();
	}

	public BlogArticle getBlogArticleByArticleID(long articleID) {
		// TODO Auto-generated method stub
		return blogArticleDao.getBlogArticleByArticleID(articleID);
	}

	public void insertBlogArticle(BlogArticle article) {
		// TODO Auto-generated method stub
		blogArticleDao.insertBlogArticle(article);
	}

	public void updateBlogArticle(BlogArticle article) {
		// TODO Auto-generated method stub
		blogArticleDao.updateBlogArticle(article);
	}

	public void deleteBlogArticle(long articleID) {
		// TODO Auto-generated method stub
		blogArticleDao.deleteBlogArticle(articleID);
	}

	public List<WordComment> getWordCommentListByWordID(long wordID) {
		// TODO Auto-generated method stub
		return wordCommentDao.getWordCommentListByWordID(wordID);
	}

	public int getWordCommentCountByWordID(long wordID) {
		// TODO Auto-generated method stub
		return wordCommentDao.getWordCommentCountByWordID(wordID);
	}

	public long insertWordComment(WordComment wordComment) {
		// TODO Auto-generated method stub
		return wordCommentDao.insertWordComment(wordComment);
	}

	public List<ReviewInfo> getReviewInfoListByToday(long lessonID) {
		// TODO Auto-generated method stub
		return lessonDao.getReviewInfoListByToday(lessonID);
	}

	public ReviewInfo getReviewInfoOfSomeDay(long lessonID, Date date) {
		// TODO Auto-generated method stub
		return lessonDao.getReviewInfoOfSomeDay(lessonID, date);
	}

	public ReviewInfo getTodayReviewInfo(long lessonID) {
		// TODO Auto-generated method stub
		return lessonDao.getTodayReviewInfo(lessonID);
	}

	public List<Word> getAllStrangeWordList(long userID) {
		// TODO Auto-generated method stub
		return wordDao.getStrangeWordListByUserID(userID);
	}

	public List<LearnedWord> getAllLearnedStrangeWordList(long userID) {
		// TODO Auto-generated method stub
		return learnedWordDao.getAllLearnedStrangeWordListByUserID(userID);
	}

	public List<Word> getNewStrangeWordList(long userID, int groupNumber) {
		// TODO Auto-generated method stub
		int offset = groupNumber * ClientUtil.STRANGE_WORD_GROUP_SIZE;
		int limit = ClientUtil.STRANGE_WORD_GROUP_SIZE;

		return wordDao.getNewStrangeWordListByUserID(userID,offset,limit);
	}

	public List<LearnedWord> getLearnedStrangeWordList(long userID,
			int groupNumber) {
		// TODO Auto-generated method stub
		int offset = groupNumber * ClientUtil.STRANGE_WORD_GROUP_SIZE;
		int limit = ClientUtil.STRANGE_WORD_GROUP_SIZE;
		
		return learnedWordDao.getLearnedStrangeWordListByUserID(
				userID, offset,limit);
	}

	public List<LearnedWord> getLearnedWordListByLessonIDAndDate(long lessonID,
			Date date) {
		// TODO Auto-generated method stub
		return learnedWordDao.getLearnedWordListByLessonIDAndDate(lessonID,date);
	}

	public void updateLesson(Lesson lesson) {
		// TODO Auto-generated method stub
		lessonDao.updateLesson(lesson);
	}

	public int getTotalLearnedWordCount(long lessonID) {
		// TODO Auto-generated method stub
		return lessonDao.getTotalLearnedWordCount(lessonID);
	}

	public int getTotalMasterWordCount(long lessonID) {
		// TODO Auto-generated method stub
		return lessonDao.getTotalMasterWordCount(lessonID);
	}
	
	public int getTotalKnowWordCount(long lessonID) {
		// TODO Auto-generated method stub
		return lessonDao.getTotalKnowWordCount(lessonID);
	}

	public int getTotalUnknowWordCount(long lessonID) {
		// TODO Auto-generated method stub
		return lessonDao.getTotalUnknowWordCount(lessonID);
	}

	public void deleteWord(long wordID) {
		// TODO Auto-generated method stub
		wordDao.deleteWord(wordID);
	}

	public List<WordLib> getAllWordLibList(int start, int limit) {
		// TODO Auto-generated method stub
		return wordLibDao.getAllUserWordLibList(start,limit);
	}

	public int getTotalWordLibCount() {
		// TODO Auto-generated method stub
		return wordLibDao.getTotalWordLibCount();
	}

	public List<Article> getAllArticleList(int start, int limit) {
		// TODO Auto-generated method stub
		return articleDao.getAllArticleList(start,limit);
	}

	public int getTotalArticleCount() {
		// TODO Auto-generated method stub
		return articleDao.getTotalArticleCount();
	}

	public List<BlogArticle> getAllBlogArticleList(int start, int limit) {
		// TODO Auto-generated method stub
		return blogArticleDao.getAllBlogArticleList(start,limit);
	}

	public int getTotalBlogArticleCount() {
		// TODO Auto-generated method stub
		return blogArticleDao.getTotalBlogArticleCount();
	}

	public void deleteCnAndSentence(long csID) {
		// TODO Auto-generated method stub
		wordDao.deleteCnAndSentence(csID);
	}

	public List<BlogArticle> getAllBlogArticleListByCategory(int start, int limit,
			String category) {
		// TODO Auto-generated method stub
		return blogArticleDao.getAllBlogArticleListByCategory(start,limit,category);
	}

	public int getTotalBlogArticleCountByCategory(String category) {
		// TODO Auto-generated method stub
		return blogArticleDao.getTotalBlogArticleCountByCategory(category);
	}

	public void insertWord(Word word) {
		// TODO Auto-generated method stub
		wordDao.insertWord(word);
	}

	public void updateWord(Word word) {
		// TODO Auto-generated method stub
		wordDao.updateWord(word);
	}

	public Word getWordByWordEnAndWordLibID(String wordEn,
			long wordLibID) {
		// TODO Auto-generated method stub
		return wordDao.getWordByWordEnAndWordLibID(wordEn,wordLibID);
	}
}
